# import time
# from multiprocessing import Process, Pool,Queue
#
# my_list = []
#
# queue
# def add_data(q: Queue):
#     for i in range(6):
#         q.put(f'数据{i}')
#         time.sleep(0.3)
#
#
# def read_data(q: Queue):
#     while True:
#         # get函数是一个阻塞函数，get函数从队列中获取一个值，并且这个值从队列中删除
#         val = q.get()
#         print(val)
#         time.sleep(0.4)
#
#
# if __name__ == '__main__':
#     q = Queue(100)
#     p1 = Process(target=add_data, args=(q,))  # 往队列中存数据
#     p2 = Process(target=read_data, args=(q,))  # 往队列中取数据
#     p1.start()
#     # p1.join()
#     p2.start()
#     # p1.terminate()
#     # p2.terminate()


# 使用pipe(管道)的方式
import time
from multiprocessing import Pipe, Process


def add_data(p: Pipe):
    for i in range(6):
        p.send(f'数据{i}')
        time.sleep(0.3)


def read_data(p: Pipe):
    while True:
        # recv函数是一个阻塞函数，
        val = p.recv()
        print(val)
        time.sleep(0.4)


if __name__ == '__main__':
    send_p, recv_p = Pipe()
    p1 = Process(target=add_data, args=(send_p,))
    p2 = Process(target=read_data, args=(recv_p,))

    p1.start()
    p2.start()
